package top.leoseven.adminproject.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import top.leoseven.adminproject.R;
import top.leoseven.adminproject.modle.ToastUtils;
import top.leoseven.adminproject.presenter.StuHomeWorkInfoPresenter;
import top.leoseven.adminproject.service.StuHomeWorkInfoService;

public class StuHomeWorkInfoActivity extends BaseActivity implements StuHomeWorkInfoService, View.OnClickListener {
    private TextView stunum;
    private EditText result,resultnum,filecontent;
    private Button commit;
    private ProgressDialog dialog;
    private StuHomeWorkInfoPresenter presenter;
    private final static int ONERROR=1001;
    private final static int ONLOADFINISH=1002;
    private final static int ONCOMMITSUCCESS=1003;
    private final static int ONCOMMITFAIL=1004;
    private int workid;

    @Override
    public void setLayout() {
        setContentView(R.layout.activity_stu_home_work_info);
        initView();
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==ONERROR) {
                dialog.dismiss();
                ToastUtils.toast(ToastUtils.CAHAN,"加载文件出错了...",StuHomeWorkInfoActivity.this, Toast.LENGTH_SHORT);
                finish();
            }else if (msg.what==ONLOADFINISH){
                dialog.dismiss();
                String s= (String) msg.obj;
                if (s!=null) {
                    filecontent.setText(s);
                }
            }else if (msg.what==ONCOMMITSUCCESS){
                //提交成功
                dialog.dismiss();
                ToastUtils.toast(ToastUtils.SE,"提交成功",StuHomeWorkInfoActivity.this,Toast.LENGTH_SHORT);
            }else if (msg.what==ONCOMMITFAIL){
                dialog.dismiss();

                ToastUtils.toast(ToastUtils.CAHAN,msg.obj.toString(),StuHomeWorkInfoActivity.this,Toast.LENGTH_SHORT);
            }
        }
    };
    private void initView() {
        stunum= (TextView) findViewById(R.id.stuid);
        result= (EditText) findViewById(R.id.result);
        resultnum= (EditText) findViewById(R.id.resultnum);
        filecontent= (EditText) findViewById(R.id.fileContent);
        commit= (Button) findViewById(R.id.commit);
        commit.setOnClickListener(this);
        dialog=new ProgressDialog(this);
        dialog.setMessage("正在加载文件");
        dialog.show();
    }

    @Override
    public void initPresenter() {
        presenter=new StuHomeWorkInfoPresenter(this,this);
        Intent intent = getIntent();
        String stuid = intent.getStringExtra("stuid");
        String result = intent.getStringExtra("result");
        float resultnum = intent.getFloatExtra("resultnum", 0f);
        this.stunum.setText(stuid);
        this.result.setText(result);
        this.resultnum.setText(resultnum+"");
        String filepath = intent.getStringExtra("filepath");
        workid = intent.getIntExtra("workid", -1);
        presenter.loadFile(filepath,workid);
    }

    @Override
    public void onFileLoadFinish(String s) {
        Message msg = Message.obtain();
        msg.what=ONLOADFINISH;
        msg.obj=s;
        handler.sendMessage(msg);
    }

    @Override
    public void onError(String e) {
        Message msg = Message.obtain();
        msg.what=ONERROR;
        msg.obj=e;
        handler.sendMessage(msg);
    }

    @Override
    public void onCommitSuccess() {
        handler.sendEmptyMessage(ONCOMMITSUCCESS);

    }

    @Override
    public void onCommitFail(String err) {
        Message msg = Message.obtain();
        msg.what=ONCOMMITFAIL;
        msg.obj=err;
        handler.sendMessage(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit:
                String p1 = this.result.getText().toString();
                Float p2 = new Float(resultnum.getText().toString());
                dialog.setMessage("正在提交数据");
                dialog.show();
                presenter.commitResult(p1,p2,workid);
                break;
        }
    }
}
